package com.sc.web.member;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sc.dal.model.ParamMap;
import com.sc.dal.model.consultComment.ConsultComment;
import com.sc.dal.model.system.UserFormMap;
import com.sc.service.consultComment.ConsultCommentService;
import com.sc.util.data.Constants;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;

/***
 * 咨询回复
 * @author xiongyong
 *
 */
@RequestMapping("/messageReply")
@Controller
public class MessageReplyController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(MessageReplyController.class);
	
	@Autowired
	private ConsultCommentService consultCommentService;
	
	
	@RequestMapping("/initMessageReply")
	public Object initMessageReply(){
		ModelAndView mav = new ModelAndView("/member/message_reply/messageReplyList");
		return mav;
	}
	
	/**
	 * 根据条件查询咨询、评价、浏览内容数据
	 * type : 1、咨询内容 2、评价晒单 3、我的足迹',
	 * @param request
	 * @return
	 */
	@RequestMapping(value="selectMessageReply")
	@ResponseBody
	public Object selectMessageReply(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		//从session里获取用户的登录信息
		/*
		 * 从session里获取用户名
		 * UserFormMap user = (UserFormMap) request.getSession().getAttribute("userSession");
		String accountName = user.get("accountName").toString();  //修改人
*/		
		String consultContent = request.getParameter("consultContent");
		ParamMap paramMap = getFormMap(ParamMap.class);
		paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
		paramMap.put("column", column);
		paramMap.put("sort", sort);
		paramMap.put("consultContent", consultContent);
		//咨询内容
		paramMap.put("type", Constants.TYPE_A);
		List<ConsultComment> goodsList = this.consultCommentService.queryMessageList(paramMap);
		pageView.setRecords(goodsList);
		return pageView;
	}
	
	/***
	 * 初始化回复页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/replyMessage")
	@ResponseBody
	public Object replyMessage(HttpServletRequest request){
		String id = request.getParameter("id");
		ConsultComment consultComment = consultCommentService.findMessageReplyById(new Integer(id));
		ModelAndView mav = new ModelAndView("/member/message_reply/reply_message");
		mav.addObject("consultComment", consultComment);
		return mav;
	}
	
	/**
	 * 回复信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editMessageReply")
	@ResponseBody
	public Object editMessageReply(HttpServletRequest request){
		try {
			UserFormMap user = (UserFormMap) request.getSession().getAttribute("userSession");
			String accountName = user.get("accountName").toString();  //修改人
			ConsultComment record = new ConsultComment();
			String consultId = request.getParameter("consultId");
			if(StringUtils.isNotBlank(consultId)){
				record.setId(new Integer(consultId));
			}
			String replyContent = request.getParameter("replyContent");
			if(StringUtils.isNotBlank(replyContent)){
				record.setReplyContent(replyContent);
			}
			record.setModifyTime(new Date());  //回复时间
			record.setReplyPeople("回复人："+accountName);
			//Status状态，1：已回复       2： 未回复
			record.setStatus("1");
			int i = consultCommentService.updateMessageReply(record);	
			if(i>0){
				return "success";
			}else{
				return "error";
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}
	
	@RequestMapping("/delMessageReply")
	@ResponseBody
	public Object delMessageReply(HttpServletRequest request){
		try {
			String ids = request.getParameter("ids");
			String[] idArr=ids.split(",");
			for (String string : idArr) {
				ConsultComment record = new ConsultComment();
				record.setId(new Integer(string));
				record.setIsDelete(Constants.IS_DELETEB);
				record.setModifyTime(new Date());
				consultCommentService.updateMessageReply(record);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("删除数据异常"+e);
			throw new ScBizException("删除数据异常！", e);
		}
		
	}
	
	

}
